package io.github.wanggit.antcms.server.service.impl.utils;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;

public abstract class DateUtil {

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static MonthRange monthRangeStr(String yyyyMM) {
        if (StringUtils.hasText(yyyyMM)) {
            try {
                DateTime dateTime = new DateTime(new SimpleDateFormat("yyyy-MM").parse(yyyyMM));
                String start = dateTime.withDayOfMonth(1).withMillisOfDay(0).toString(DATE_PATTERN);
                String end =
                        dateTime.plusMonths(1)
                                .withDayOfMonth(1)
                                .withMillisOfDay(0)
                                .toString(DATE_PATTERN);
                return new MonthRange(start, end);
            } catch (Exception e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return new MonthRange();
    }

    public static class MonthRange {
        private String start;
        private String end;

        MonthRange() {}

        MonthRange(String start, String end) {
            this.start = start;
            this.end = end;
        }

        public String getStart() {
            return start;
        }

        public String getEnd() {
            return end;
        }
    }
}
